<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\AttendanceGroup;
use App\Models\PersonnelDynamics;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class AttendanceGroupController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->ag_id) {
            $attendance_group = AttendanceGroup::findOrFail($request->ag_id);
            $attendance_group->mtime = time();
        } else {
            $attendance_group = new AttendanceGroup();
            $attendance_group->ctime = time();
        }
        $attendance_group->ag_name = $request->ag_name;
        $attendance_group->time_type = $request->time_type;
        $attendance_group->company_id = $company_id;
        $attendance_group->ag_type = $request->ag_type;
        $attendance_group->ag_standard_duration = $request->ag_standard_duration;
        $attendance_group->auto_attendance = $request->auto_attendance;
        $attendance_group->employee_work_duty = $request->employee_work_duty;
        $attendance_group->attendance_condition = $request->attendance_condition;
        $attendance_group->attendance_place = $request->attendance_place;
        $attendance_group->attendance_wifi = $request->attendance_wifi;
        $attendance_group->attendance_outdoor = $request->attendance_outdoor;
        $attendance_group->ag_mon = $request->ag_mon;
        $attendance_group->ag_tue = $request->ag_tue;
        $attendance_group->ag_wed = $request->ag_wed;
        $attendance_group->ag_thu = $request->ag_thu;
        $attendance_group->ag_fri = $request->ag_fri;
        $attendance_group->ag_sat = $request->ag_sat;
        $attendance_group->ag_sun = $request->ag_sun;
        $attendance_group->legal_holidays = $request->legal_holidays;
        $attendance_group->special_days = $request->special_days;
        $attendance_group->organization_ids = $request->organization_ids;
        $attendance_group->leader_id = $request->leader_id;
        $attendance_group->employee_ids = $request->employee_ids;
        $attendance_group->uid = $uid;

        $attendance_group->save();

        return showError(Error::ERR_OK,['ag_id'=>$attendance_group->ag_id]);

    }

    public function show(Request $request) {
        $attendance_group = AttendanceGroup::findOrFail($request->ag_id);
        return showError(Error::ERR_OK,['attendance_group'=>$attendance_group]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = AttendanceGroup::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        AttendanceGroup::where('ag_id',$request->ag_id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
